package com.biostime.qdingding.modules.qd.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.modules.qd.api.dto.CouponDto;
import com.biostime.qdingding.modules.qd.dao.CouponDao;
import com.biostime.qdingding.modules.qd.entity.Coupon;

/**
 * 优惠券Service
 * @author king
 * @version 2016-02-17
 */
@Service
@Transactional(readOnly = true)
public class CouponService extends CrudService<CouponDao, Coupon> {

	public List<CouponDto> myCoupons(String userId,String type,String centerId){
		return dao.myCoupons(userId,type,centerId);
	}
	public int availableNum(String userId,String type){
		return dao.availableNum(userId, type);
	}
	
	@Override
	public List<Coupon> findList(Coupon entity) {
		if(entity != null){
			entity = new Coupon();
		}
		dataScopeFilter(entity, "dsf", "id=a.center_id", "id=a.create_by");
		
		return super.findList(entity);
	}
	
	@Override
	public Page<Coupon> findPage(Page<Coupon> page, Coupon entity) {
		entity.setPage(page);
		List<Coupon> coupons = this.findList(entity);
		for(Coupon coupon : coupons){
			determination(coupon);
		}
		page.setList(coupons);
		return page;
	}
	
	public Coupon determination(Coupon coupon){
		StringBuffer buffer = new StringBuffer();
		String useRange = coupon.getUseRange();
		if(!StringUtils.isEmpty(useRange)){
			String[] useRanges = useRange.split("\\|");
			for(String temp : useRanges){
				switch (temp) {
				case "0":
					buffer.append("免费体验课  ");
					coupon.getUseRangeBoolean().set(0, true);
					break;
				case "1":
					buffer.append("普通课程  ");
					coupon.getUseRangeBoolean().set(1, true);
					break;
				case "2":
					buffer.append("户外课程  ");
					coupon.getUseRangeBoolean().set(2, true);
					break;
				default:
					break;
				}
			}
			coupon.setUseRange(buffer.toString());
			if(coupon.getUseRange().equals("")){
				coupon.setUseRange("无限制");
			}
		}
		return coupon;
	}
	
	public Coupon selectByMemberCouponId(String couponId){
		return dao.selectByMemberCouponId(couponId);
	}
	
		
	public int useCoupon(String couponId , String useState){
		return dao.useCoupon(couponId,useState);
	}
	
	public List<Coupon> findGiftList(Coupon coupon){
		return dao.findGiftList(coupon);
	}
}
